package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseDao;
import com.lagou.dao.impl.CourseDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.service.CourseService;
import com.lagou.utils.DateUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CourseServiceImpl implements CourseService {

    private CourseDao courseDao = null;

    public CourseServiceImpl() {
        this.courseDao = new CourseDaoImpl();
    }

    @Override
    public List<Course> findCourseList() {
        return courseDao.findCourseList();
    }

    @Override
    public List<Course> findByCourseNameAndStatus(String courseName, String status) {

        return courseDao.findByCourseNameAndStatus(courseName, status);
    }

    @Override
    public String saveCourseSalesInfo(Course course) {

        String date = DateUtils.getDateFormat();
        course.setCreate_time(date);
        course.setUpdate_time(date);
        course.setStatus(1);

        int row = courseDao.saveCourseSalesInfo(course);
        String result = null;
        if (row > 0) {
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }

        return result;
    }

    @Override
    public Course findCourseById(int id) {
        return courseDao.findCourseById(id);
    }

    @Override
    public String updateCourseSaleInfo(Course course) {
        int row = courseDao.updateCourseSaleInfo(course);
        String result = null;
        if (row > 0) {
            result = StatusCode.SUCCESS.toString();
        } else {
            result = StatusCode.FAIL.toString();
        }
        return result;
    }

    @Override
    public Map<String, Integer> updateCourseStatus(Course course) {

        int row = courseDao.updateCourseStatus(course);
        Map<String, Integer> map = new HashMap<>();
        if (row > 0) {
            if (course.getStatus() == 0) {
                map.put("status", 0);
            } else {
                map.put("status", 1);
            }
        }

        return map;
    }
}
